﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using Process.Entities;

namespace Process.DAL
{
    public class CategoryDAL
    {
        private static CategoryENT CreateCategoryFromReader(IDataReader reader)
        {
            CategoryENT item = new CategoryENT();

            if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.CategoryID.ToString()))) item.CategoryID = Utility.StringParse(reader[CategoryColumns.CategoryID.ToString()]);
			if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.CategoryName.ToString()))) item.CategoryName = Utility.StringParse(reader[CategoryColumns.CategoryName.ToString()]);
			if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.ParentCategoryID.ToString()))) item.ParentCategoryID = Utility.StringParse(reader[CategoryColumns.ParentCategoryID.ToString()]);

            return item;
        }

        public static DataTable GetCategoryList(string parentCategoryID)
        {
            try
            {
                DataAccessHelper.AddParameter("ParentCategoryID", SqlDbType.VarChar, parentCategoryID);
                return  DataAccessHelper.ExecuteQueryDataTable("procCategoryGetList");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhóm sản phẩm.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}
